<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);

class manage extends admin{
	private $db;
	
	public $site;
	
	function __construct() {
		parent::__construct();
		pc_base::load_app_class('global');
		$this->db = pc_base::load_model('talent_model');//加载model
		$this->admin_db = pc_base::load_model('admin_model');
		$this->experience_db = pc_base::load_model('experience_model');
		$this->experience_type_db = pc_base::load_model('experience_type_model');
		$this->talent_source_db = pc_base::load_model('talent_source_model');
		$this->video_db = pc_base::load_model('videos_model');
		$this->user_info_db = pc_base::load_model('user_info_model');//加载用户model
		$this->audit_db = pc_base::load_model('audit_model');//加载用户认证model
		$this->category_source_db = pc_base::load_model('category_source_model');
		$this->tag_model = pc_base::load_model('variable_model');//加载APP变量model
		$this->site = get_siteid();//获取当前站点
		$this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
		$model_setting=pc_base::load_model('site_setting_model');
		$this->talent_source= $model_setting->getValue("talent_source",$this->site);//获取人才来源
		$this->talent_xl = $model_setting->getValue("talent_xl",$this->site);//获取人才学历
		$this->continent= $model_setting->getValue("talent_continent",$this->site);//获取人才所在州
		$this->nation = $model_setting->getValue("talent_nation",$this->site);//获取人才国家
		$city = $model_setting->getValue("city",$this->site);//获取城市
		$province = $model_setting->getValue("talent_province",$this->site);//获取人才省份
		$new_city = array();
		foreach ($city as $k=>$v){
			$new_city[$v] = $v;
		}
		$this->city = $new_city;
		$company = $model_setting->getValue("company",$this->site);//获取人才所属公司
		$new_company = array();
		foreach ($company as $k=>$v){
			$new_company[$v] = $v;
		}
		$this->company= $new_company;
		$talent_province = array();
		foreach ($province as $v){
			$talent_province[$v] = $v;
		}
		$this->province = $talent_province;
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		if ($_GET['talent_type'] == '1') {
			$where.= "is_moter = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '2') {
			$where.= "is_student = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '3') {
			$where.= "is_hot = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '4') {
			$where.= "is_guest = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '5') {
			$where.= "is_judge = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '6') {
			$where.= "is_star = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '7') {
			$where.= "is_mentor = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '8') {
			$where.= "is_missworld = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '9') {
			$where.= "is_agent = 1 and status = 1 and siteid=".$this->site;
		} elseif ($_GET['talent_type'] == '10') {
			$where.= "is_business = 1 and status = 1 and siteid=".$this->site;
		}
		if ($_GET['keyword'] || $_GET['address'] || $_GET['model_card_img'] || $_GET['model_card_imgs'] || $_GET['source']) {			if (!empty($_GET['keyword'])) {
				if ($_GET['key'] == 1) {
					$keyword = $_GET['keyword'];
					$where.= " and real_name like '%$keyword%'";
				} elseif ($_GET['key'] == 2) {
					$keyword = $_GET['keyword'];
					$where.= " and telephone = '$keyword'";
				} elseif ($_GET['key'] == 3) {
					$keyword = $_GET['keyword'];
					$where.= " and identification = '$keyword'";
				}
			}
			if ($_GET['address']) {
				$address = $_GET['address'];
				$where.=" and address LIKE '%$address%'";
			}
			//是否有模卡
			if ($_GET['model_card_img']) {
				$model_card_img = $_GET['model_card_img'];
				if($_GET['model_card_img']==1){
				    $where.=" and model_card_img !=''";
				}
			}
			//是否有模卡组图
			if ($_GET['model_card_imgs']) {
				$model_card_imgs = $_GET['model_card_imgs'];
				if($_GET['model_card_imgs']==1){
				    $where.=" and model_card_imgs !=''";
				}
			}			if ($_GET['source'] !="") {				$source = $_GET['source'];
				$where .= " and source = ".$source;
			}			$talent_info = $this->db->listinfo($where,'follow DESC,praise DESC',$page,10);//人才搜索按照点赞人数、关注人数由高到低排列
		} else {
			$talent_info = $this->db->listinfo($where,'id DESC',$page,10);
		}
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();
		foreach ($talent_info as $k=>$v){
			//获取用户管理员真实名称数据
			$talent_info[$k]['username'] = $adminInfo[$v['updated_by']];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		$talent_type = $_GET['talent_type'] ? intval($_GET['talent_type']) : 1;
		if ($talent_type == '1') {
			include $this->admin_tpl('moter_list','moter');
		} elseif ($talent_type == '2') {
			include $this->admin_tpl('student_list','student');
		} elseif ($talent_type == '3') {
			include $this->admin_tpl('network_list','network');
		} elseif ($talent_type == '4') {
			include $this->admin_tpl('guest_list','guest');
		} elseif ($talent_type == '5') {
			include $this->admin_tpl('judges_list','judges');
		} elseif ($talent_type == '6') {
			include $this->admin_tpl('star_list','star');
		} elseif ($talent_type == 7 || $talent_type == 9 || $talent_type == 10) {
			include $this->admin_tpl('teacher_list','teacher');
		}elseif ($talent_type == '8') {
			include $this->admin_tpl('missworld_list','missworld');
		}
	}
	
	public function add() {
		$talent_type = $_GET['talent_type'];
		if (isset($_POST['dosubmit'])) {
			if (empty($_POST['talent']['talent_type']) || $_POST['talent']['talent_type'] == '') {
				showmessage(L('talent_type_must'),HTTP_REFERER);
			}
			$model_card_imgs_url = $_POST['model_card_imgs_url']?$_POST['model_card_imgs_url']:'';
			if($model_card_imgs_url){
			    $_POST['talent']['model_card_imgs'] = implode(',', $model_card_imgs_url);
			}else{
			    $_POST['talent']['model_card_imgs'] = '';
			}
			$result = $this->db->moter_add($_POST['talent'],$talent_type);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:green;">添加人才</span>[人才id：'.$result.'，姓名：'.$_POST['talent']['real_name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('add_success'), '?m=talent&c=manage&a=init&talent_type='.$talent_type.'&menuid='.$_POST['talent']['menuid']);
			} else {
				showmessage(L('add_error'),HTTP_REFERER);
			}
		} else {
			pc_base::load_sys_class('form', '', 0);
			pc_base::load_sys_class('format', '', 0);
			if ($talent_type == 1) {
				$tag_info = $this->tag_model->getValue('model_tag');
				include $this->admin_tpl('moter_add','moter');
			} elseif ($talent_type == 2) {
				include $this->admin_tpl('student_add','student');
			} elseif ($talent_type == 3) {
				include $this->admin_tpl('network_add','network');
			} elseif ($talent_type == 4) {
				include $this->admin_tpl('guest_add','guest');
			} elseif ($talent_type == 5) {
				include $this->admin_tpl('judges_add','judges');
			} elseif ($talent_type == 6) {
				include $this->admin_tpl('star_add','star');
			} elseif ($talent_type == 7) {
				include $this->admin_tpl('teacher_add','teacher');
			} elseif ($_GET['talent_type'] == '8') {
				include $this->admin_tpl('missworld_add','missworld');
			}
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
        extract($info);
        $status = trim($_REQUEST['status']);
        //模卡组图字符串转数组
        $pictureurls = explode(',', $model_card_imgs);
		$talent_type = $_GET['talent_type'];
		if (isset($_POST['dosubmit'])) {
			$model_card_imgs_url = $_POST['model_card_imgs_url']?$_POST['model_card_imgs_url']:'';
			if($model_card_imgs_url){
			    $_POST['talent']['model_card_imgs'] = implode(',', $model_card_imgs_url);
			}else{
			    $_POST['talent']['model_card_imgs'] = '';
			}
			$up_info = isset($_POST['talent']) ? $_POST['talent'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
// 			$condition = "";
// 			$condition .= "id == ".$id;
// 			$exist = $this->db->get_one($condition);
// 			if ($exist) {
// 				showmessage(L('talent_exist'),HTTP_REFERER);
// 			}
// 			if (empty($_POST['talent']['talent_type']) || $_POST['talent']['talent_type'] == '') {
// 				showmessage(L('talent_type_must'),HTTP_REFERER);
// 			}
			$result = $this->db->moter_edit($up_info,$talent_type,$id);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改人才信息</span>[人才id：'.$id.'，姓名：'.$_POST['talent']['real_name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('edit_success'), '?m=talent&c=manage&a=init&talent_type='.$talent_type.'&menuid='.$_POST['talent']['menuid'].'&pc_hash='.$_SESSION['pc_hash']);
			} else {
				showmessage(L('edit_error'),HTTP_REFERER);
			}
		} else {
			pc_base::load_sys_class('form', '', 0);
			pc_base::load_sys_class('format', '', 0);
			$show_header = false;
			if ($talent_type == 1) {
				$tag_info = $this->tag_model->getValue('model_tag');
				include $this->admin_tpl('moter_edit','moter');
			} elseif ($talent_type == 2) {
				include $this->admin_tpl('student_edit','student');
			} elseif ($talent_type == 3) {
				include $this->admin_tpl('network_edit','network');
			} elseif ($talent_type == 4) {
				include $this->admin_tpl('guest_edit','guest');
			} elseif ($talent_type == 5) {
				include $this->admin_tpl('judges_edit','judges');
			} elseif ($talent_type == 6) {
				include $this->admin_tpl('star_edit','star');
			} elseif ($talent_type == 7) {
				include $this->admin_tpl('teacher_edit','teacher');
			} elseif ($_GET['talent_type'] == '8') {
				include $this->admin_tpl('missworld_edit','missworld');
			}
		}
	}
	
	public function del() {
		$talent_type = $_GET['talent_type'];
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message not exits'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$where = "id in ('$id')";
		$talent_name = $this->db->select($where,'real_name','','id DESC');
		foreach ($talent_name as $v){
			$name[] = $v['real_name'];
		}
		if ($talent_type == 1 || $talent_type == 9 || $talent_type == 10) {
			$del_user = $this->user_info_db->original_user($id);
			if (!$del_user) {
				showmessage(L('del_error'), HTTP_REFERER);
			}
			$del_auth_user = $this->audit_db->original_authuser($id);
			if (!$del_auth_user) {
				showmessage(L('del_error'), HTTP_REFERER);
			}
		}
		$result = $this->db->update(array(
				'status' => '2'
		), "id in ('$id')");//删除人才
		$result_experience = $this->experience_db->update(array(
				'status'=>'2'
		),"talent_id in ('$id')");//删除人才关联履历
		$result_source = $this->talent_source_db->deleteTalentsource($id);//删除人才关联的资讯、视频、图片集
		$result_video = $this->video_db->deleteTalentvideo($id);//删除人才关联的视频集
		$result_cate_resource = $this->category_source_db->deleteTalentcategoryResource($id);//删除人才关联的栏目
		if ($result && $result_experience && $result_source && $result_video && $result_cate_resource) {
			$this->refreshData(6);
			$this->refreshData(16);
			$this->refreshData(13);
			$del_id = explode('\',\'', $id);
			$new_id = implode('，', $del_id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除人才</span>[人才id：'.$new_id.',人才姓名：'.json_encode($name,JSON_UNESCAPED_UNICODE).']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_success'), '?m=talent&c=manage&a=init&talent_type='.$talent_type.'&menuid='.$_GET['menuid']);
		} else {
			showmessage(L('del_error'), HTTP_REFERER);
		}
	}
	
	public function detail() {
		$talent_type = $_GET['talent_type'];
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		if ($talent_type == 1) {
			include $this->admin_tpl('moter_detail','moter');
		} elseif ($talent_type == 2) {
			include $this->admin_tpl('student_detail','student');
		} elseif ($talent_type == 3) {
			include $this->admin_tpl('network_detail','network');
		} elseif ($talent_type == 4) {
			include $this->admin_tpl('guest_detail','guest');
		} elseif ($talent_type == 5) {
			include $this->admin_tpl('judges_detail','judges');
		} elseif ($talent_type == 6) {
			include $this->admin_tpl('star_detail','star');
		} elseif ($talent_type == 7 || $talent_type == 8 || $talent_type == 9 || $talent_type == 10) {
			include $this->admin_tpl('teacher_detail','teacher');
		}
	}
	
	/**
	 * 添加人才校验身份证号，防止人才重复
	 * @date: 2017年3月15日 上午9:57:24
	 * @author: LIYANG
	 */
	public function check_talent() {
		$taletn_idnum = $_GET['identification'];
		if (isset($taletn_idnum)){
			$id = $this->db->get_one(array('identification'=>$taletn_idnum,'siteid'=>$this->site,'status'=>1));
			if ($id['identification'] == $taletn_idnum) {
				exit('0');
			} else {
				exit('1');
			}
		} else {
			exit('0');
		}
	}
	
	/**
	 * 根据真实姓名获取姓名首字母
	 */
	public function auto_letter() {
		pc_base::load_app_func('getFirstCharter','sign_up');
		$name = trim($_GET['realname']);
		$name = substr($name, 0 , 3);
		$letter = getFirstCharter($name);
		if (isset($letter)) {
			exit($letter);
		} else {
			exit('undefined');
		}
	}
	
	/**
	 * 删除人才刷新缓存
	 */
	private function refreshData($type) {
		file_get_contents(API_URL.'/xinsilu/v1/refreshRedis/refreshRedis?type='.$type);
	}
	
	/**
	 * 是否显示在APP端（ajax更改状态）
	 */
	public function change_app_status() {
		$data = array();
		if (! isset($_POST['id']) || empty($_POST['id'])) {
			showmessage(L('message not exits'), HTTP_REFERER);
		}
		$id = $_POST['id'];
		$info = $this->db->get_one(array('id'=>$id),'real_name');
		$status = $_POST['status'] ? intval($_POST['status']) : 0;
		$result = $this->db->update(array('is_display_app'=>$status),array('id'=>$id));
		if ($result) {
			//记录行为日志
			if ($status == 0) {
				$log_content = '<span style="color:red;">取消人才在APP端展示</span>[人才id：'.$id.',人才姓名：'.$info['real_name'].']';
			} else {
				$log_content = '<span style="color:green;">人才在APP端展示</span>[人才id：'.$id.',人才姓名：'.$info['real_name'].']';
			}
			$this->manage_admin_log($log_content);
			
			$data['status'] = '200';
			$data['info'] = 'success';
		} else {
			$data['status'] = '201';
			$data['info'] = 'error';
		}
		echo json_encode($data);
		exit();
		
	}
}